﻿using MapleLeaf.Pay.Pay;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;

namespace MapleLeaf.Client.Pages {

    public class RecordPageViewModel : INotifyPropertyChanged {


        public event PropertyChangedEventHandler PropertyChanged;


        public IEnumerable<HongshPayTrade> Trades { get; set; }

        public int PageLength => (int)Math.Ceiling(HongshPayClientContext.Default.TradeManager.Count / (double)PagePerLength);


        public int PageIndex {
            get {
                return _PageIndex;
            }
            set {
                _PageIndex = value;
                Trades = HongshPayClientContext.Default.TradeManager.Skip((PageIndex - 1) * PagePerLength).Take(PagePerLength);
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PageIndex"));
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Trades"));
            }
        }

        public RecordPageViewModel() {
            _PageIndex = 1;
            HongshPayClientContext.Default.TradeManager.CollectionChanged += TradeManager_CollectionChanged;
        }

        private void TradeManager_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("PageLength"));
            PageIndex = PageIndex;
        }


        static RecordPageViewModel() {
            PagePerLength = 10;
        }

        private static readonly int PagePerLength;

        private int _PageIndex;
    }
}
